Passed
Push — master ( dcfba0...0e0426 )
by MusikAnimal
04:50
created

autoedits.js ➔ ... ➔ $.done   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
$(function () {
2
    var editOffset = 0;
3
4
    if ($('body.autoedits').length) {
5
        setupToggleTable(window.countsByTool, window.toolsChart, 'count', function (newData) {
6
            var total = 0;
7
            Object.keys(newData).forEach(function (tool) {
8
                total += parseInt(newData[tool].count, 10);
9
            });
10
            var toolsCount = Object.keys(newData).length;
11
            $('.tools--tools').text(
12
                toolsCount.toLocaleString() + " " +
13
                $.i18n('num-tools', toolsCount)
14
            );
15
            $('.tools--count').text(total.toLocaleString());
16
        });
17
    }
18
19
    // Contributions table has already been loaded, so set up listeners.
20
    if ($('.contribs-table')[0]) {
21
        setupNavListeners();
22
    }
23
24
    /**
25
     * Loads non-automated edits from the server and lists them in the DOM.
26
     * The navigation aids and showing/hiding of loading text is also handled here.
27
     */
28
    window.loadContributions = function () {
29
        $('.non-auto-edits-loading').show();
30
        $('.non-auto-edits-container').hide();
31
        var project = $('.non-auto-edits-container').data('project'),
32
            username = $('.non-auto-edits-container').data('username'),
33
            start = $('.non-auto-edits-container').data('start'),
34
            end = $('.non-auto-edits-container').data('end'),
35
            namespace = $('.non-auto-edits-container').data('namespace'),
36
            target = $('.non-auto-edits-container').data('target');
37
38
        /** global: xtBaseUrl */
39
        $.ajax({
40
            url: xtBaseUrl + target + '-contributions/' + project + '/' + username + '/' +
41
                namespace + '/' + start + '/' + end + '/' + editOffset + '?htmlonly=yes&' +
42
                location.search.slice(1), // Append tool=foo parameter, if present.
43
            timeout: 30000
44
        }).done(function (data) {
45
            $('.non-auto-edits-container').html(data).show();
46
            $('.non-auto-edits-loading').hide();
47
            setupNavListeners();
48
49
            if (editOffset > 0) {
50
                $('.prev-edits').show();
51
            }
52
            if ($('.contribs-table tbody tr').length < 50) {
53
                $('.next-edits').hide();
54
            }
55
        }).fail(function (_xhr, _status, message) {
56
            $('.non-auto-edits-loading').hide();
57
            $('.non-auto-edits-container').html(
58
                $.i18n('api-error', 'Non-automated edits API: <code>' + message + '</code>')
59
            ).show();
60
        });
61
    }
62
63
    /**
64
     * Set up listeners for navigating non-automated contributions
65
     */
66
    function setupNavListeners()
67
    {
68
        $('.prev-edits').on('click', function (e) {
69
            e.preventDefault();
70
            editOffset -= 50;
71
            loadContributions()
72
        });
73
74
        $('.next-edits').on('click', function (e) {
75
            e.preventDefault();
76
            editOffset += 50;
77
            loadContributions();
78
        });
79
    }
80
});
81